//
//  SliderVoiceView.m
//  NewMusic
//
//  Created by dllo on 15/11/17.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "SliderVoiceView.h"

@interface SliderVoiceView ()

@end

@implementation SliderVoiceView

- (void)dealloc
{
    [_slider release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubView];
    }
    return self;
}
- (void)creatSubView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, self.frame.size.height)];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.1;
    [self addSubview:view];
    [view release];
    
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(-105, 195, self.frame.size.height * 13 / 21, 30)];
    self.slider.transform = CGAffineTransformMakeRotation(M_PI / 2 * 3);
    self.slider.maximumValue = 10;
    self.slider.minimumValue = 0;
    [self.slider setThumbImage:[UIImage imageNamed:@"BF-dian"] forState:UIControlStateNormal];
    self.slider.maximumTrackTintColor = [UIColor colorWithRed:100 / 255.0 green:100 / 255.0 blue:100 / 255.0 alpha:1.0];
    self.slider.minimumTrackTintColor = [UIColor colorWithRed:200 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    CGFloat vol = [user floatForKey:@"voice"];
    self.slider.value = vol;
    [self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.slider];
    [_slider release];
    
    // x-10 y-80
    UIImageView *imageViewVolMax = [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 30, self.frame.size.height / 14)];
    imageViewVolMax.image = [UIImage imageNamed:@"BF-volMax"];
    imageViewVolMax.userInteractionEnabled = YES;
    [self addSubview:imageViewVolMax];
    [imageViewVolMax release];
    
    UIImageView *imageViewVolMin = [[UIImageView alloc] initWithFrame:CGRectMake(15, 350, 30, self.frame.size.height / 14)];
    imageViewVolMin.userInteractionEnabled = YES;
    imageViewVolMin.image = [UIImage imageNamed:@"BF-volmin"];
    [self addSubview:imageViewVolMin];
    [imageViewVolMin release];
    
    UITapGestureRecognizer *tapMax = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(MaxImageViewAction)];
    [imageViewVolMax addGestureRecognizer:tapMax];
    [tapMax release];
    UITapGestureRecognizer *tapMin = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(MinImageViewAction)];
    [imageViewVolMin addGestureRecognizer:tapMin];
    [tapMin release];
    
}
- (void)sliderAction:(UISlider *)sender
{
    [self.delegate VolSliderAction:sender.value];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setFloat:sender.value forKey:@"voice"];
}
- (void)MaxImageViewAction
{
    [self.delegate VolSliderAction:10];
    [self.slider setValue:10 animated:YES];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setFloat:10 forKey:@"voice"];
}
- (void)MinImageViewAction
{
    [self.delegate VolSliderAction:0];
    [self.slider setValue:0 animated:YES];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setFloat:0 forKey:@"voice"];
    
}
@end
